<template>
  <!-- 当点击 取消按钮 时，关闭弹窗 -->
  <div>
    <el-dialog :title="$t('mesProdOtherCode.reprint')" :visible.sync="visible" width="70%" top="4vh" :append-to-body="true" @close="clearDialog()" v-loading="loding" :close-on-click-modal="false">
      <div>
        <ele-page
          :otherParams="otherParams"
          :formConfig="searchConfig"
          ref="eleTable"
          :operateList="operateList"
          @handleOperateEvent="handleOperateEvent"
          :column="column"
          :getList="mesProdOthercodeReprint"
        >
        </ele-page>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="clearDialog()">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
    <el-dialog width="20%" :title="$t('mesProdOtherCode.print')" :visible.sync="innerVisible" :append-to-body="true" @close="clearFormDialog()" v-loading="loding" :close-on-click-modal="false">
      <el-form :model="form">
        <el-form-item :label="$t('common.cancel')" label-width="85px">
          <el-input size="small" disabled v-model="form.printingTimes"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clearFormDialog()">{{ $t('common.cancel') }}</el-button>
        <el-button type="primary" @click="handleRequest">{{ $t('common.submit') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDict } from '@/api/common'
import { printTemplateInfoName } from '@/api/barcodeprint/mesBarcodeprintHistory'

import { mesProdOthercodeReprint, selectMesProdOthercodeReprintDetail, mesProdOthercodeReprintList } from '@/api/print/mesProdOthercode'
export default {
  name: 'repairPrintTable',
  data() {
    let that = this
    return {
      innerVisible: false,
      loding: false,
      printRow: {},
      form: { printingTimes: 0 },
      otherParams: { barcode: '', codeType: '' },
      loding: false,
      visible: false,
      mesProdOthercodeReprint,
      operateList: [
        {
          name: $i18n.t('common.add'),
          type: 'primary',
          icon: 'el-icon-circle-plus-outline',
          handleName: 'add' //点击时返回的标识
        }
      ],
      searchConfig: {
        inline: true,
        formDesc: {
          status: {
            layout: 7,
            type: 'select',
            label: $i18n.t('common.status'),
            type: 'select',
            prop: {
              value: 'value',
              text: 'description'
            },
            options: async (data) => {
              let res = await getDict('mes_barcode_reprint_status')
              return res.data.result || []
            }
          }
          // isPrint: {
          //   layout: "6",
          //   type: "select",
          //   label: "是否打印",
          //   type: "select",
          //   prop: {
          //     value: "value",
          //     text: "description",
          //   },
          //   options: async (data) => {
          //     let res = await getDict("yes_or_not");
          //     return res.data.result || [];
          //   },
          // },
        }
      },
      column: [
        {
          type: 'selection'
        },
        {
          type: 'index',
          label: $i18n.t('common.order'),
          width: 60,
          fixed: 'left'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.orderCode'),
          prop: 'orderCode'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.codeType'),
          prop: 'codeType'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.startBarcode'), //"补打起始唯一码",
          prop: 'startBarcode'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.endBarcode'),
          prop: 'endBarcode'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.printTemp'), //"打印模板",
          prop: 'printTemp'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.createByName'), //"申请人",
          prop: 'createByName'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.createTime'), // "申请时间",11
          prop: 'createTime'
        },
        {
          label: $i18n.t('common.status'), //"状态",
          prop: 'status',
          optionProp: {
            value: 'value',
            label: 'description'
          },
          option: async (data) => {
            let res = await getDict('mes_barcode_reprint_status')
            return res.data.result || []
          }
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.examineByName'), // "审核人",
          prop: 'examineByName'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.examineTime'), // "审核时间",
          prop: 'examineTime'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.examineContent'), // "审核内容",
          prop: 'examineContent'
        },
        {
          label: $i18n.t('mesOtherCodePrintingRecord.isPrint'), //"是否打印",
          prop: 'isPrint',
          optionProp: {
            value: 'value',
            label: 'description'
          },
          option: async (data) => {
            let res = await getDict('yes_or_not')
            return res.data.result || []
          }
        },
        {
          prop: 'operate',
          label: $i18n.t('common.operation'),
          width: 160,
          fixed: 'right',
          render: (h, { row }) => {
            let dom = <p></p>
            if (row.status == 'examinePass') {
              dom = (
                <el-button
                  onClick={() => {
                    that.print(row)
                  }}
                  type="primary">
                  {$i18n.t('mesOtherCodePrintingRecord.reprint')}
                </el-button>
              )
            }
            return dom
          }
        }
      ],
      tableData: [],
      params: {}
    }
  },
  methods: {
    //初始化
    init(params) {
      console.log('2222', params)
      this.params = params
      this.otherParams = {
        isPrint: 0,
        barcode: params.barcode,
        codeType: params.codeType
      }
      this.visible = true
      this.refresh()
    },
    refresh() {
      this.$nextTick(() => {
        this.$refs.eleTable.refresh()
      })
    },

    clearDialog() {
      this.visible = false
      this.$emit('refreshList')
    },
    // 表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name
      const fnc = {
        add: () => {
          const data = {
            orderCode: this.params.orderCode,
            codeType: this.params.codeType,
            startBarcode: this.params.barcode,
            endBarcode: this.params.barcode
          }
          mesProdOthercodeReprintList(data).then((res) => {
            this.refresh()
          })
        }
      }
      fnc[data.handleName]()
    },
    //点击打印按钮
    async handleRequest() {
      let res = await printTemplateInfoName({
        templateName: this.printRow.printTemp
      })

      let data = await selectMesProdOthercodeReprintDetail(this.printRow.id)
      let barcodeInfo = {
        ...res.data.result[0],
        values: data.data.result
      }
      // ----条码打印多份（条码数量根据打印次数printingTimes重新计算）----
      let newInfoList = []
      barcodeInfo.values.forEach((v) => {
        newInfoList = [...newInfoList, ...Array(Number(this.params.printingTimes)).fill(v)]
      })
      barcodeInfo.values = newInfoList
      localStorage.splitPrint = JSON.stringify(barcodeInfo)
      this.visible = false
      this.$parent.refresh(false)
      window.open('/mom/component/mes/splitPrint?type=10')
    },
    async print(row) {
      this.printRow = row
      this.innerVisible = true
    }
  }
}
</script>
